<template>
	<view>
		<!-- 头部导航 -->
		<page-header :headInfo="headInfo"></page-header>
		<form @submit="formSubmit">
			<view class="cu-form-group margin-top">
				<view class="title">标题</view>
				<input placeholder="图片标题" name="title"></input>
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">分类</view>
				<picker @change="PickerChange" :value="index" :range="picker" name="classify">
					<view class="picker">
						{{index>-1?picker[index]:'请选择'}}
					</view>
				</picker>
			</view>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]" name="classify">
					 <image :src="imgPath + imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="upLoadImg" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			
			<view><button class="round bg-blue shadow" form-type="submit" style="margin: 30rpx;" :loading="isLoading" :disabled="isLoading">提交</button></view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgPath:this.imgPath,
				headInfo:{
					title:'上传图片',
				},
				index: 0,
				picker: ['请选择','情侣头像', '动漫风景', '动漫角色','壁纸','萌宠','手绘'],
				imgList: [],
				isLoading:false
			}
		},
		onLoad() {
		},
		/**
		 * @author smallfour
		 * @description 分享好友
		 */
		onShareAppMessage(res){
			return {
				title:'四叶图集',
				imageUrl:'http://image.hermit.love/IMG_3494.JPG',
				path:'/pages/tabBar/index'
			}
		},
		/**
		 * @author smallfour
		 * @description 分享朋友圈
		 */
		onShareTimeline(){
			return {
				title:'四叶图集',
				imageUrl:'http://image.hermit.love/IMG_3494.JPG',
				path:'/pages/tabBar/index'
			}
		},
		
		methods: {
			/**
			 * @author smallfour
			 * @description 上传图片
			 */
			upLoadImg:function(){
				let that = this;
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
						let imgFile = res.tempFilePaths[0];
						that.$api.uploadImg({file:imgFile}).then(res => {
							console.log(res)
							let arr = [];
							arr.push(res.data.unprefix_imgUrl);
							that.imgList = arr;
						}).catch(err => {
							console.log(err);
						})
				    }
				});
			},
			
		
			/**
			 * @author smallfour
			 * @description 选择分类
			 */
			PickerChange(e) {
				this.index = e.detail.value
			},
		
			/**
			 * @author smallfour
			 * @description 查看图片
			 */
			ViewImage(e) {
				let that = this;
				let imgArr = [];
				for(let i in that.imgList){
					imgArr.push(this.imgPath + that.imgList);
				}
				uni.previewImage({
					urls: imgArr,
					longPressActions:{
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
		
			/**
			 * @author smallfour
			 * @description 删除图片
			 */
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这张图片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			
					
			/**
			 * @author smallfour
			 * @description 提交
			 */
			formSubmit:function(e){
				if(!Number(e.detail.value.classify)){
					this.msg('请选择分类')
					return;
				}
				if(this.imgList.length < 1){
					this.msg('请上传图片')
					return;
				}
				this.isLoading = true;
				let atlasObj = e.detail.value;
				atlasObj.imgUrl = this.imgList[0];
				this.$api.atalsAdd(atlasObj).then(res => {
					if(res.code === 200){
						this.msg('提交成功');
						setTimeout(function(){
							this.isLoading = false;
							uni.navigateBack();
						},500)
					}
				}).catch(err => {
					console.log(err);
				})
			}
		}
	}
</script>

<style>
	
</style>
